package org.ichamigua.user.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.ichamigua.user.mapper.UserInfoMapper;
import org.ichamigua.user.service.UserInfoService;
import org.ichihamigua.common.framework.entity.Result;
import org.ichihamigua.common.qiniucloud.service.QiniuObjectStoreService;
import org.ichihamigua.common.qiniucloud.vo.UploadFileVo;
import org.ichihamigua.common.user.dto.*;
import org.ichihamigua.common.user.entity.UserInfoEntity;
import org.ichihamigua.common.user.vo.UserInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author rainkyzhong
 * @date 2023/10/31 22:06
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfoEntity> implements UserInfoService {

    private final static String USER_BACKGROUND_FILE_BUCKET_NAME = "ichihamigua";


    @Resource
    private QiniuObjectStoreService qiniuObjectStoreService;

    @Override
    public Result<UserInfoVo> details(Long userId) {
        UserInfoEntity userInfo = getById(userId);
        if(userInfo == null){
            return Result.fail("用户不存在");
        }
        return Result.success(new UserInfoVo(
                userInfo.getBackgroundUrl(),
                userInfo.getGender(),
                userInfo.getCity(),
                userInfo.getBirthday(),
                userInfo.getPersonalSignature()
        ));
    }

    @Override
    public Result<?> updatePersonalSignature(Long userId, UpdatePersonalSignatureDto dto) {
        UserInfoEntity userInfo = new UserInfoEntity();
        userInfo.setUserId(userId);
        userInfo.setPersonalSignature(dto.getValue());
        if (!updateById(userInfo)) {
            return Result.fail("修改失败");
        }
        return Result.success("修改成功");
    }

    @Override
    public Result<?> updateGender(Long userId, UpdatePersonalGenderDto dto) {
        UserInfoEntity userInfo = new UserInfoEntity();
        userInfo.setUserId(userId);
        userInfo.setGender(dto.getValue());
        if (!updateById(userInfo)) {
            return Result.fail("修改失败");
        }
        return Result.success("修改成功");
    }


    /**
     * 查询用户信息
     * @param userId 用户ID
     * @param columns 查询的字段
     * @return 查出该用户指定的用户信息
     */
    @Override
    public UserInfoEntity getOne(Long userId, List<SFunction<UserInfoEntity, ?>> columns){
        LambdaQueryWrapper<UserInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfoEntity::getUserId, userId);
        if(columns != null ) wrapper.select(columns);
        return getOne(wrapper);
    }

    /**
     * 查询用户信息
     * @param userId 用户ID
     * @return 查出该用户所有的用户信息
     */
    @Override
    public UserInfoEntity getOne(Long userId){
        return getOne(userId, null);
    }

    @Override
    public Result<?> updateBirthday(Long userId, UpdateBirthdayDto dto) {
        //查询用户信息
        UserInfoEntity userInfo = getOne(
                userId, List.of(UserInfoEntity::getUserId, UserInfoEntity::getBirthday)
        );
        if(userInfo == null){
            return Result.fail("用户不存在");
        }
        userInfo.setBirthday(dto.getValue());
        if (!updateById(userInfo)) {
            return Result.fail("修改失败");
        }
        return Result.success("修改成功");
    }

    @Override
    public Result<?> updateCity(Long userId, UpdateCityDto dto) {
        UserInfoEntity userInfo = new UserInfoEntity();
        userInfo.setUserId(userId);
        userInfo.setCity(dto.getValue());
        if (!updateById(userInfo)) {
            return Result.fail("修改失败");
        }
        return Result.success("修改成功");
    }

    @Override
    public Result<?> updateBackground(Long userId, MultipartFile file) {
        //上传封面
        Result<UploadFileVo> upload = qiniuObjectStoreService.upload(
                USER_BACKGROUND_FILE_BUCKET_NAME,
                String.format("background-%d-%d.jpg", userId, System.currentTimeMillis()),
                file
        );
        if(!upload.getSuccess()){
            return upload;
        }
        String url = "ic_background://"+upload.getData().getFileName();

        //修改封面
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setUserId(userId);
        userInfoEntity.setBackgroundUrl(url);
        if (!save(userInfoEntity)){
            return Result.fail("修改失败");
        }
        return Result.success();
    }


}
